package ink.lovejinhu.leetcode;

import java.util.HashMap;

/**
 * @author jinhu
 * created 2021-07-07 9:50
 */
public class Solution_0219 {
    /**
     * 判断数组中是否有重复元素2
     * <p>
     * <p>
     * 判断是否有两数num[i]=num[j],且i与j的相差至多为k
     *
     * @param nums
     * @param k
     * @return
     */
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        HashMap<Integer, Integer> temp = new HashMap<Integer, Integer>();
        for (int i = 0; i < nums.length; i++) {
            if (temp.containsKey(nums[i])) {
                if (Math.abs(temp.get(nums[i]) - i) <= k)
                    return true;
                else temp.put(nums[i], i);
            } else
                temp.put(nums[i], i);

        }
        return false;
    }
}
